package cc.sheji.Service;


import cc.sheji.domain.Love;
import cc.sheji.domain.LoveQueryCriteria;
import cc.sheji.domain.PictureCollection;
import cc.sheji.domain.User;
import cc.sheji.reposity.LoveRepository;
import cc.sheji.reposity.PictureCollectionRepository;
import cc.sheji.reposity.UserRepository;
import cc.sheji.util.QueryHelp;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.*;

@Service
public class LoveService {

    @Autowired
    LoveRepository loveRepository;
    @Autowired
    PictureCollectionRepository pictureCollectionRepository;
    @Autowired
    UserRepository userRepository;
    public Love create(Love entity) {
        entity.setId(null);
        entity.setUserId(StpUtil.getLoginIdAsLong());
        PictureCollection pictureCollection = pictureCollectionRepository.findById(entity.getPictureCollectionId()).get();
        Integer loveNum = pictureCollection.getLoveNum();
        if(loveNum==null)
            loveNum=0;
        pictureCollection.setLoveNum(loveNum+1);
        pictureCollectionRepository.save(pictureCollection);
        loveRepository.save(entity);
        User user = userRepository.findById(pictureCollection.getUserId()).get();
        Integer loveNum1 = user.getLoveNum();
        if(loveNum1==null)
            loveNum1=0;
        user.setLoveNum(loveNum1+1);
        userRepository.save(user);
        return entity;
    }

    public Love get(Long id) throws InterruptedException {
        Love entity = loveRepository.findById(id).get();
        return entity;
    }

    public Love update(Love entity) {
        entity = loveRepository.save(entity);
        return entity;
    }

    @Transactional
    public boolean delete(Love entity) {
        System.out.println(entity.getPictureCollectionId());
        loveRepository.deleteAllByPictureCollectionIdAndUserId(entity.getPictureCollectionId(), StpUtil.getLoginIdAsLong());
        PictureCollection pictureCollection = pictureCollectionRepository.findById(entity.getPictureCollectionId()).get();
        Integer loveNum = pictureCollection.getLoveNum();
        if(loveNum==null)
            loveNum=0;
        pictureCollection.setLoveNum(loveNum-1);
        pictureCollectionRepository.save(pictureCollection);
        User user = userRepository.findById(pictureCollection.getUserId()).get();
        Integer loveNum1 = user.getLoveNum();
        user.setLoveNum(loveNum1-1);
        userRepository.save(user);
        return true;
    }


    public List<Love> list(LoveQueryCriteria criteria, Pageable pageable) {
        Sort order = Sort.by(Sort.Direction.ASC, "createTime");
        order = Sort.by(Sort.Direction.DESC, "createTime");
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), order);
        Page<Love> page = loveRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return page.getContent();
    }

}
